package com.beyond.trace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.domain.TraceProduct;
import com.beyond.trace.mapper.TraceProductMapper;
import com.beyond.trace.mapper.TraceScanStatisticsMapper;
import com.beyond.trace.service.ITraceProductService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceProductServiceImpl extends ServiceImpl<TraceProductMapper, TraceProduct> implements ITraceProductService
{
    @Resource
    private TraceProductMapper traceProductMapper;

    @Resource
    private TraceScanStatisticsMapper scanStatisticsMapper;

    /**
     * 查询产品信息
     * 
     * @param productId 产品信息ID
     * @return 产品信息
     */
    @Override
    public TraceProduct selectTraceProductById(Long productId, HttpServletRequest request)
    {
        try {
            QueryWrapper<TraceProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id",productId);
            try {  //解决未传token的问题
//                if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//                    queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
//                }
            }catch (Exception e) {
                e.printStackTrace();
            }
            return traceProductMapper.selectOne(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 查询产品对应的基地信息
     *
     * @param productId 产品信息ID
     * @return 基地信息
     */
    @Override
    public TraceBase selectTraceProductBase(Long productId)
    {
        return traceProductMapper.selectTraceProductBase(productId);
    }

    /**
     * 查询产品信息列表
     * 
     * @param traceProduct 产品信息
     * @return 产品信息
     */
    @Override
    public List<TraceProduct> selectTraceProductList(TraceProduct traceProduct)
    {
        QueryWrapper<TraceProduct> queryWrapper = new QueryWrapper<>(traceProduct);
        try {
//            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//                queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
//            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return traceProductMapper.selectList(queryWrapper);
    }

    /**
     * 新增产品信息
     * 
     * @param traceProduct 产品信息
     * @return 结果
     */
    @Override
    public boolean insertTraceProduct(TraceProduct traceProduct)
    {
        traceProduct.setCreateTime(DateUtils.getNowDate());
        traceProduct.setUpdateTime(DateUtils.getNowDate());
        traceProduct.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProduct.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProduct.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        if(StringUtils.isEmpty(traceProduct.getProductNo())) {
            String id = SnowUtil.getId();
            String no = DateUtils.getDate().replace("-", "") + id.substring(id.length() - 6);
            traceProduct.setProductNo(no);
        }
        //return traceProductMapper.insertTraceProduct(traceProduct);
        return super.save(traceProduct);
    }

    /**
     * 修改产品信息
     * 
     * @param traceProduct 产品信息
     * @return 结果
     */
    @Override
    public int updateTraceProduct(TraceProduct traceProduct)
    {
        traceProduct.setUpdateTime(DateUtils.getNowDate());
       // traceProduct.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceProductMapper.updateById(traceProduct);
    }

    /**
     * 批量删除产品信息
     * 
     * @param productIds 需要删除的产品信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductByIds(Long[] productIds) throws Exception
    {

        Map<String, Integer> checkMap = traceProductMapper.checkProductUse(Arrays.asList(productIds));

        if(StringUtils.isNotEmpty(checkMap)) {
            throw new Exception("产品已经在使用不能删除："+ String.join(",",checkMap.keySet()));
        }

        QueryWrapper<TraceProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("product_id", Arrays.asList(productIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductMapper.delete(queryWrapper);
    }

    /**
     * 删除产品信息信息
     * 
     * @param productId 产品信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductById(Long productId)
    {
        QueryWrapper<TraceProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductMapper.delete(queryWrapper);
    }


}
